package com.example.reactor;

import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

public class CreationExample {
    public static void main(String[] args) {
        // 示例 1: 使用 Mono 创建操作符
        Mono<String> monoJust = Mono.just("Hello, Mono");
        Mono<String> monoEmpty = Mono.empty();
        Mono<String> monoDefer = Mono.defer(() -> Mono.just("Deferred Mono"));

        // 订阅 Mono 并打印结果
        monoJust.subscribe(System.out::println);
        monoEmpty.subscribe(System.out::println, Throwable::printStackTrace, () -> System.out.println("Completed"));
        monoDefer.subscribe(System.out::println);

        // 示例 2: 使用 Flux 创建操作符
        Flux<String> fluxJust = Flux.just("A", "B", "C");
        Flux<String> fluxFromArray = Flux.fromArray(new String[]{"A", "B", "C"});
        List<String> list = Arrays.asList("A", "B", "C");
        Flux<String> fluxFromIterable = Flux.fromIterable(list);
        Flux<String> fluxFromStream = Flux.fromStream(Stream.of("A", "B", "C"));
        Flux<Integer> fluxRange = Flux.range(1, 5);
        Flux<Long> fluxInterval = Flux.interval(Duration.ofSeconds(1));
        Flux<String> fluxDefer = Flux.defer(() -> Flux.just("Deferred Flux"));

        // 订阅 Flux 并打印结果
        fluxJust.subscribe(System.out::println);
        fluxFromArray.subscribe(System.out::println);
        fluxFromIterable.subscribe(System.out::println);
        fluxFromStream.subscribe(System.out::println);
        fluxRange.subscribe(System.out::println);
        fluxInterval.take(5).subscribe(System.out::println);
        fluxDefer.subscribe(System.out::println);
    }
}
